import { request } from '@/utils/request'
import type { UserStudy, UserFavorite } from '@/types/common'

export const userStudyApi = {
  // 获取用户学习记录列表
  getStudyRecords(page = 1, size = 10, orderBy = 'createTime') {
    return request.get<{ list: UserStudy[], total: number, page: number, size: number, totalPages: number }>('/study/list', {
      params: { page, size, orderBy }
    })
  },

  // 获取学习记录详情
  getStudyRecordDetail(id: number) {
    return request.get<UserStudy>(`/study/${id}`)
  },

  // 创建或更新学习记录
  saveStudyRecord(data: Partial<UserStudy>) {
    return request.post<UserStudy>('/study/record', data)
  },

  // 获取用户学习进度统计
  getStudyProgress() {
    return request.get('/study/progress')
  },

  // 获取特定教程的学习记录
  getTutorialStudyRecord(tutorialId: number) {
    return request.get<UserStudy>(`/study/tutorial/${tutorialId}`)
  },

  // 删除学习记录
  deleteStudyRecord(id: number) {
    return request.delete(`/study/${id}`)
  },

  // 批量删除学习记录
  batchDeleteStudyRecords(ids: number[]) {
    return request.delete('/study/batch', {
      data: ids
    })
  }
}

export const userFavoriteApi = {
  // 获取用户收藏列表
  getFavoriteList() {
    return request.get<UserFavorite[]>('/favorite/list')
  },

  // 添加或取消收藏
  toggleFavorite(targetType: string, targetId: number) {
    return request.post('/favorite/toggle', null, {
      params: { targetType, targetId }
    })
  },

  // 检查是否已收藏
  checkFavorite(targetType: string, targetId: number) {
    return request.get('/favorite/check', {
      params: { targetType, targetId }
    })
  }
}
